<?php


namespace App\Repositories;


use App\Enums\RoomEnum;
use App\Services\Tools\RedisService;

class AdRepository
{
    // 获取首页轮播图列表
    public function getBannersList($user_id = 0)
    {
        $allowUserIds = [813602, 984434, 707302]; // 当前用户ID在放行数组中，则展示所有轮播图
        if ($user_id > 0 && in_array($user_id, $allowUserIds)) {
            $where = [];
        } else {
            $where['display'] = 1;
        }

        $where['category_id'] = 1;
        $list                 = app("model_banner")->where($where)->select('id as ad_id', 'type', 'title', 'item_id', 'link_url', 'content', 'detail_pictures')->orderBy('sort', 'asc')->get()->toArray();

        if ($list) {
            foreach ($list as $key => $val) {
                if ($val['detail_pictures']) {
                    $list[$key]['detail_pictures'] = explode(';', $val['detail_pictures']);
                } else {
                    $list[$key]['detail_pictures'] = [];
                }

                if ($val['type'] == 3 && $val['link_url'] && defined('TOKEN')) {
                    if (strpos($val['link_url'], '?') !== false) {
                        $list[$key]['link_url'] = $val['link_url'];
                    } else {
                        $list[$key]['link_url'] = $val['link_url'];
                    }
                }
                $list[$key]['room_type'] = 0;
                if ($val['type'] == 1 && $val['item_id']) {    //房间获取房间类型
                    $list[$key]['room_type'] = RoomEnum::clientRoomType(RedisService::getRoomDetailByRoomId($val['item_id'], 'sys_type_id'));
                }
            }
        }

        return array_values($list);
    }
}
